﻿using System.Collections.Generic;
using System.ComponentModel;

namespace PersonaLibrary.Search
{
	public class SearchItemModel : INotifyPropertyChanged
	{
		private readonly List<string> _binaryOperators = new List<string> { "ИЛИ", "И" };
		private List<BaseSearchProperty> _properties = new List<BaseSearchProperty>();
		private string _selectedBinaryOperator;
	    private BaseSearchProperty _selectedProeprty;
		private bool _binaryEnable = true;

		public bool BinaryEnable
		{
			get { return _binaryEnable; }
			set
			{
				_binaryEnable = value;
				RisePropertyChanged("BinaryEnable");
			}
		}

		public string SelectedBinaryOperator
		{
			set
			{
				_selectedBinaryOperator = value;
				RisePropertyChanged("SelectedBinaryOperator");
			}
			get
			{
				return _selectedBinaryOperator;
			}
		}
		
		public List<string> BinaryOperators
		{
			get
			{
				return _binaryOperators;
			}
		}

		public System.Linq.Expressions.Expression CreateBinary(System.Linq.Expressions.Expression left, System.Linq.Expressions.Expression right)
		{ 
			System.Linq.Expressions.Expression result = null;

			switch (_selectedBinaryOperator)
			{
				case "И":
					result = System.Linq.Expressions.Expression.And(left, right);
					break;
				case "ИЛИ":
					result = System.Linq.Expressions.Expression.Or(left, right);
					break;
			}

			return result;
		}

	    public BaseSearchProperty SelectedProperty
	    {
	        set 
            { 
                _selectedProeprty = value;
                RisePropertyChanged("SelectedProperty");
            }
            get { return _selectedProeprty; }
	    }

		public List<BaseSearchProperty> Properties
		{
			get { return _properties; }

			set
			{
				_properties = value;
				if(_properties != null && _properties.Count > 0)
				{
					_selectedProeprty = _properties[0];
				}
			}
		}

		public event PropertyChangedEventHandler PropertyChanged;

		protected void RisePropertyChanged(string propertyName)
		{
			if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
		}
	}
}
